package com.shop.controller.admin;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import com.jf.core.BaseController;
import com.jfinal.kit.PathKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.IAtom;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.shop.bean.NzcpdlBean;
import com.shop.bean.ProPicsBean;
import com.shop.bean.UserBean;
import com.shop.bean.UserInfoBean;
import com.shop.utils.MD5Utils;
import com.shop.utils.StringUtils;

public class AdminController extends BaseController {
	
	/**
	 * 后台登录页面
	 */
	public void dolgn(){
		render(WEB_INF_URL+"admin/dolgn.jsp");
	}
	
	/**
	 * 后台登录操作
	 */
	public void adidn(){
		String uname = getPara("textfield");
		String pwd = getPara("textfield2");
		
		String sql = StringUtils.getSql("sql.find.findUserInfoByUserName",uname);
		UserBean user = UserBean.dao.findFirst(sql);  // ujuese = 3 是管理员
		if(user !=null && "3".equals(user.getStr("ujuese")) && MD5Utils.makeMD5Password(pwd, user.getStr("uencry"))
						.equals(user.getStr("upass"))){
			logger.info("管理员登录："+uname);
			setSessionAttr("aduser", user);
			user.set("lasttime", StringUtils.getFormatTime(new Date())).update();
			render(WEB_INF_URL+"admin/adindex.jsp?url="+StringUtils.getTimeCode());
		}else{
			renderHtml("<script>alert('用户名或密码错误');history.back();</script>");
		}
		
	}
	
	/**
	 * 获取大类列表
	 * */
	public void getCpdlList(){
		String sql_ = StringUtils.getSql("sql.find.findCpdlList");
		List<NzcpdlBean> list = NzcpdlBean.cpdlDao.find(sql_);
		System.out.println(list);
		renderJson("cpdlList",list);
	}
	
	/** 
	 * 获取管理员列表
	 * */
	public void getAdminList(){
		String select = "select tblyh.*,tblyhxx.*";
		String sqlExceptSelect = "from tblyonghu tblyh inner join tblyhxinxi tblyhxx on tblyh.ucode=tblyhxx.ucode and tblyh.ujuese=3";
		Integer page = getParaToInt("page");
		if(page == null || "".equals(page)){
			page= 1;
		}
		Page<Record> adminList = Db.paginate(page, 55, select,sqlExceptSelect);
		setAttr("users", adminList);
		render(WEB_INF_URL+"admin/files/listadmins.jsp");
	}
	
	/**
	 * 禁用所有选择的用户
	 * */
	public void delUserList(){
		final String ucodeList = getPara("ucodeList");
		boolean bool = Db.tx(new IAtom(){
			@Override
			public boolean run() throws SQLException {
				boolean result = true;
				String sql_ = "";
				String[] ucodeArr = ucodeList.split("@");
				for(String ucode: ucodeArr){
					sql_ = StringUtils.getSql("sql.find.findUserInfoByCode", ucode);
					result = UserInfoBean.userInfoDao.findFirst(sql_).set("ustate", 0).update();
					if(!result){
						break;
					}
				}
				return result;
			}
		});
		if(bool){
			renderJson("message","true");
		}else{
			renderJson("message","false");
		}
	}
	
	/**
	 * 激活所有选择的用户
	 * */
	public void jhUserList(){
		final String ucodeList = getPara("ucodeList");
		boolean bool = Db.tx(new IAtom(){
			@Override
			public boolean run() throws SQLException {
				boolean result = true;
				String sql_ = "";
				String[] ucodeArr = ucodeList.split("@");
				for(String ucode: ucodeArr){
					sql_ = StringUtils.getSql("sql.find.findUserInfoByCode", ucode);
					result = UserInfoBean.userInfoDao.findFirst(sql_).set("ustate", 1).update();
					if(!result){
						break;
					}
				}
				return result;
			}
		});
		if(bool){
			renderJson("message","true");
		}else{
			renderJson("message","false");
		}
	}
	
	/**
	 * 进入管理员信息修改页面
	 * */
	public void goToEditDetail(){
	    String ucode = getPara("ucode");
	    String sql_ = StringUtils.getSql("sql.find.findUserInfoByCode", ucode);
	    Record record = Db.findFirst(sql_);
	    setAttr("user",record);
	    render(WEB_INF_URL+"admin/files/editadmin.jsp");
	}
	
	/**
	 * 修改管理员信息
	 * */
	public void editAdmin(){
		String flag = getPara("flag");
		String ucode = getPara("ucodeVal");
		String uname = getPara("uname");
		String newPwd = getPara("newPwd");
		boolean bool = false;
		if(flag.equals("0")){
			bool = UserBean.dao.findById(ucode).set("uname", uname).update();
		}else{
			
			UserBean user = UserBean.dao.findById(ucode);
			String encry = user.getStr("uencry");
			String pwd = MD5Utils.makeMD5Password(newPwd, encry);
			bool = user.set("uname", uname).set("upass", pwd).update();
		}
		if(bool){
			redirect("getAdminList");
		}else{
			
		}
	}
	
	
//----------------  首页管理  ------------------------------------//	
	//图片切换  5张 循环
	public void homepics(){
		String sql = "select * from tblpics pc  where pc.pcode = ? order by pc.piccode DESC LIMIT 5";
		List<ProPicsBean> list = ProPicsBean.dao.find(sql,"101");//默认的 首页图片  五张切换
		setAttr("pclist",list);
		
		String sql2 = "select * from tblpics pc  where pc.pcode = ? order by pc.piccode DESC LIMIT 3";
		List<ProPicsBean> list2 = ProPicsBean.dao.find(sql2,"102");//默认的 首页图片 三张列表
		setAttr("pclist2",list2);
		
		render(WEB_INF_URL+"admin/files/homepics.jsp");
	}
	//上传首页 切换图片
	public void douploadpic(){
		String pcs = super.uploadFile("pcfile","homeimg");
		String pichref = getPara("pichref");
		Record rd = new Record();
		rd.set("piccode", StringUtils.getTimeCode())
			.set("pcode", "101")//默认的 首页图片  切换
			.set("picurl", pcs) 
			.set("pichref",pichref);
		if(Db.save("tblpics", rd)){
			homepics();
		}else{
			setAttr("result", "图片上传失败。");
		}
	}
	//删除 首页图片
	public void removepic(){
		String piccode = getPara("piccode");
		ProPicsBean pic = ProPicsBean.dao.findById(piccode);
		String picurl = pic.getStr("picurl");
		if(StringUtils.deleteFile(PathKit.getWebRootPath()+picurl) && pic.delete()){
			homepics();
		}else{
			setAttr("result", "图片删除失败。");
		}
	}
	
	//上传首页  列表图片
	public void douploadpic2(){
		String pcs = super.uploadFile("pcfile","homeimg");
		String pichref = getPara("pichref");
		Record rd = new Record();
		rd.set("piccode", StringUtils.getTimeCode())
			.set("pcode", "102")//默认的 首页图片   左边列表
			.set("picurl", pcs)
			.set("pichref",pichref);
		if(Db.save("tblpics", rd)){
			homepics();
		}else{
			setAttr("result", "图片上传失败。");
		}
	}
	
}
